package com.eqdd.yiqidian.kotlin.utils

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.utils.ClickUtil
import com.eqdd.yiqidian.utils.DensityUtil
import com.xujiaji.happybubble.BubbleDialog
import kotlinx.android.synthetic.main.list_item_85.view.*

/**
 * Created by jhj on 18-9-12.
 */
object PopWindowUtil {

    fun popMenu(context: Context, list: List<String>, view: View, body: (bubbleDialog: BubbleDialog, position: Int) -> Unit) {

        val inflater = LayoutInflater.from(context)
        val rootView = inflater.inflate(R.layout.list_item_85, null, false)
        rootView.recyclerView.layoutManager = LinearLayoutManager(context)
        rootView.recyclerView.addItemDecoration(DividerLineItemDecoration(context)
                .setLeftPadding(DensityUtil.percentW(10))
                .setRightPadding(DensityUtil.percentW(10)))
        SlimAdapterEx.create().register<String>(R.layout.list_item_86, SlimInjector<String> { data, injector ->
            injector?.text(R.id.tv_content, data)
        }).attachTo(rootView.recyclerView).updateData(list)

        val bubbleDialog = BubbleDialog(context)
                .addContentView<BubbleDialog>(rootView.rootView)
                .setClickedView<BubbleDialog>(view)
                .setPosition<BubbleDialog>(BubbleDialog.Position.BOTTOM)
                .calBar<BubbleDialog>(true)

        ItemClickSupport.addTo(rootView.recyclerView).setOnItemClickListener { recyclerView, position, v ->
            bubbleDialog.dismiss()
            body(bubbleDialog, position)
        }
        ClickUtil.click(view) { bubbleDialog.show() }
    }

}